﻿using Cocon90.Lib.Dal.Store;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Demo
{
    public partial class BaseStoreDemo : Form
    {
        Cocon90.Lib.Dal.IDataHelper db = Cocon90.Lib.Dal.DataHelperFactory.CreateInstence();
        IStore<Model.Address> store = new BaseStore<Model.Address>();
        Cocon90.Lib.Dal.Tools.ModelHelper<Model.Address> modelHelper = new Cocon90.Lib.Dal.Tools.ModelHelper<Model.Address>();
        public BaseStoreDemo()
        {
            InitializeComponent();
            store.IsAsc = true;
            //store.PageCountOneTimeLoad =3;
            //store.PageSize = 20;
            store.OnDataChanged += store_OnDataChanged;
            TempDataTable = db.getTable("select * from Person.Address");

            foreach (var prop in typeof(Model.Address).GetProperties())
            {
                cbColumn.Items.Add(prop.Name);
            }
        }

        void store_OnDataChanged(object data, object staticData, int pageNumber, int pageSize, int staticPageNumber, int staticPageSize)
        {
            dgv.DataSource = data;

            lbInfo.Text = "StaticPageNumber:" + store.StaticPageNumber + "|StaticPageSize:" + store.StaticPageSize;
            lbPage.Text = "PageNumber：" + store.PageNumber + "|PageSize:" + store.PageSize;
            lbInfoRight.Text = "记录总数：" + store.GetAllRowCount();
        }


        private void loadData_Click(object sender, EventArgs e)
        {
            store.Load();
        }
        private void prevPage_Click(object sender, EventArgs e)
        {

            store.PageNumber--;
            store.Load();
        }

        private void nextPage_Click(object sender, EventArgs e)
        {
            store.PageNumber++;
            store.Load();
        }
        DataTable TempDataTable { get; set; }
        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            store.LoadFrom(TempDataTable);
        }

        private void 通过数据表的上一页ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (store.PageNumber >= 2)
            {
                store.PageNumber--;
            }
            store.LoadFrom(TempDataTable);
        }

        private void 通过数据表的下一页ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            store.PageNumber++;
            store.LoadFrom(TempDataTable);
        }

        private void filterToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //定义过滤功能
            store.FilterFunction += (index, propertie, value) =>
            {
                //只显示选中列中包含过滤条件的。
                return propertie == cbColumn.Text && (value + "").Contains(txtSearch.Text);
                //只显示每页中的前10条记录
                //return (propertie == cbColumn.Text) && (index % store.PageSize) <= 10;
            };
            store.Load(true);//传入True表示启用过滤的加载。
            //store.LoadFrom(TempDataTable,true);//传入True表示启用过滤的加载。
        }

        private void 设置为自定义SQLToolStripMenuItem_Click(object sender, EventArgs e)
        {
            store.SelectSQLString = "select top 85 * from " + modelHelper.GetTableName();
            store.Load();
        }


    }
}
